package com.kh.mychart;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class LineBarChart extends View {
    public LineBarChart(Context context) {
        super(context);
    }

    public LineBarChart(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public LineBarChart(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    float outRadius = 8;
    float innerRadius = 6;

    public SparseArray<MonthValue> map;

    public float barWidth = 15;
    public boolean drawBar = false;
    public float textHeight = 14;
    /**
     * 垫底高,点至少这么高，字也是从这个基础上面抬高的
     */
    public float baseY = 50;
    public float markerX = 70;
    private Path path = new Path();

    @Override
    protected void onDraw(Canvas canvas) {
        int width = getWidth();
        int height = getHeight();
        canvas.translate(0, height);
        Paint paint = new Paint();
        paint.setAntiAlias(true);//抗锯齿
        paint.setColor(Color.RED);

        int size = map.size();
        for (int i = 0; i < size; i++) {
            MonthValue currentYear = map.get(i + 1);
            float currentY = currentYear.current.y;
            drawPoint(canvas, width, paint, currentY);
            drawLine(canvas, width, paint, currentYear, currentY);
            if (drawBar) drawBar(canvas, width, paint, currentY);
        }
        drawTextLabel(canvas, paint, map);
    }

    public float lowerHeight = 25f;

    private void drawTextLabel(Canvas canvas, Paint paint, SparseArray<MonthValue> map) {
        //字要最后画
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(3);
        paint.setTextSize(30f);
        paint.setColor(Color.BLACK);
        //todo 比较到底，低的更低，高的更高
        int size = map.size();
        List<MonthValue> nodes = new ArrayList<>();
        List<Float> needSorts = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            MonthValue e = map.get(1 + i);
            nodes.add(e);
            needSorts.add(e.current.y);
        }
        Float max = Collections.max(needSorts);
        Float min = Collections.min(needSorts);
        int maxIndex = needSorts.indexOf(max);
        int minIndex = needSorts.indexOf(min);
        MonthValue maxNode = nodes.get(maxIndex);
        MonthValue minNode = nodes.get(minIndex);
        MonthValue.Point p1 = maxNode.current;
        float maxY = p1.y;
        canvas.drawText(p1.markText, markerX, -(maxY + baseY + textHeight), paint);
        if (size > 1) {
            MonthValue.Point p2 = minNode.current;
            float minY = p2.y;
            canvas.drawText(p2.markText, markerX, -(minY + baseY - textHeight - lowerHeight), paint);
        }
    }

    private void drawBar(Canvas canvas, int width, Paint paint, float currentY) {
        paint.setColor(Color.RED);
        paint.setStrokeWidth(barWidth);
        paint.setStyle(Paint.Style.STROKE);
        float firstPointX = width / 2;
        path.moveTo(firstPointX, 0);
        path.lineTo(firstPointX, -(currentY + baseY));                      // lineTo
        path.close();
        canvas.drawPath(path, paint);
    }

    private void drawLine(Canvas canvas, int width, Paint paint, MonthValue lineNode, float currentY) {
        MonthValue.Point next = lineNode.next;
        if (null != next) {//不是最后一段就画
            float nextY = next.y;
            paint.setColor(Color.BLACK);
            canvas.drawLine(width / 2, -(currentY + baseY), width / 2 * 3, -(nextY + baseY), paint);
        }
        MonthValue.Point pre = lineNode.pre;
        if (null != pre) {
            float preY = pre.y;
            paint.setColor(Color.BLACK);
            canvas.drawLine(width / 2, -(currentY + baseY), -width / 2, -(preY + baseY), paint);
        }
    }

    private void drawPoint(Canvas canvas, int width, Paint paint, float currentY) {
        canvas.drawCircle(width / 2, -(currentY + baseY), outRadius, paint);

        paint.setColor(Color.WHITE);
        canvas.drawCircle(width / 2, -(currentY + baseY), innerRadius, paint);
    }
}
